﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;
using LW.DB;
using System.Data.SqlClient;

namespace iLangl.LanguageWire.DAL.iLanglDAL.XMLStorage
{
	public class CompanyDAL: BaseStorageDAL, ICompanyDAL
	{
		#region ICompanyDAL Members

		public void CreateCompany(Company company, IDbTransaction transaction)
		{
			//Check if company exists
			dtoXMLStorage.CompanyRow companyRow = Storage.Company.FindById(company.Id);

			bool isNew = (companyRow == null);
			if (isNew)
			{
				companyRow = Storage.Company.NewCompanyRow();
			}

			companyRow.Id = company.Id;

			if (!string.IsNullOrEmpty(company.Name))
				companyRow.Name = company.Name;

			if (!string.IsNullOrEmpty(company.Address))
				companyRow.Address = company.Address;

			if (!string.IsNullOrEmpty(company.City))
				companyRow.City = company.City;

			if (!string.IsNullOrEmpty(company.Country))
				companyRow.Country = company.Country;

			if (!string.IsNullOrEmpty(company.Currency))
				companyRow.Currency = company.Currency;

			if (!string.IsNullOrEmpty(company.Phone))
				companyRow.Phone = company.Phone;

			if (!string.IsNullOrEmpty(company.State))
				companyRow.State = company.State;

			if (!string.IsNullOrEmpty(company.Zip))
				companyRow.Zip = company.Zip;


			if (!string.IsNullOrEmpty(company.TPIdentity))
				companyRow.TPIdentity = company.TPIdentity;

			if (isNew)
			{
				Storage.Company.AddCompanyRow(companyRow);
				companyRow.TimeSpan = CurrentTimeSpan;
				UpdateDeliveryTime(eEntityType.Company, company.Id, companyRow.TimeSpan);
			}

			if (company.BillingInfo != null)
			{
				//Save Billing Info
				dtoXMLStorage.BillingInfoRow billingrow = Storage.BillingInfo.FindById(company.BillingInfo.Id);

				isNew = (billingrow == null);

				if (isNew)
				{
					billingrow = Storage.BillingInfo.NewBillingInfoRow();
				}

				BillingInfo bi = company.BillingInfo;

				billingrow.Id = bi.Id;
				billingrow.CompanyId = company.Id;

				if (!string.IsNullOrEmpty(bi.Name))
					billingrow.Name = bi.Name;

				if (!string.IsNullOrEmpty(bi.BillingFirstName))
					billingrow.BillingFirstName = bi.BillingFirstName;

				if (!string.IsNullOrEmpty(bi.BillingLastName))
					billingrow.BillingLastName = bi.BillingLastName;

				if (!string.IsNullOrEmpty(bi.BillingEmail))
					billingrow.BillingEmail = bi.BillingEmail;

				if (!string.IsNullOrEmpty(bi.BillingCountry))
					billingrow.BillingCountry = bi.BillingCountry;

				if (!string.IsNullOrEmpty(bi.BillingStreetAddress))
					billingrow.BillingStreetAddress = bi.BillingStreetAddress;

				if (!string.IsNullOrEmpty(bi.BillingStreetAddress2))
					billingrow.BillingStreetAddress2 = bi.BillingStreetAddress2;

				if (!string.IsNullOrEmpty(bi.BillingPostalCode))
					billingrow.BillingPostalCode = bi.BillingPostalCode;

				if (!string.IsNullOrEmpty(bi.BillingCity))
					billingrow.BillingCity = bi.BillingCity;

				if (!string.IsNullOrEmpty(bi.BillingCurrency))
					billingrow.BillingCurrency = bi.BillingCurrency;

				if (!string.IsNullOrEmpty(bi.TPIdentity))
					billingrow.TPIdentity = bi.TPIdentity;


				if (isNew)
				{
					Storage.BillingInfo.AddBillingInfoRow(billingrow);

					billingrow.TimeSpan = CurrentTimeSpan;
					UpdateDeliveryTime(eEntityType.BillingInfo, bi.Id, billingrow.TimeSpan);
				}

			}

			if (transaction == null)
			{
				AcceptChanges();
			}

		}


		public Company ReadCompany(string companyId)
		{
			dtoXMLStorage.CompanyRow companyRow = Storage.Company.FindById(companyId);

			if (companyRow == null)
				return null;

			Company company = new Company();
			company.Id = companyRow.Id;
			company.Name = companyRow.Name;
			company.Address = companyRow.Address;
			company.City = companyRow.City;
			company.Country = companyRow.Country;
			company.Currency = companyRow.Currency;
			company.Phone = companyRow.Phone;
			company.State = companyRow.State;
			company.Zip = companyRow.Zip;

			if (!companyRow.IsTPIdentityNull())
				company.TPIdentity = companyRow.TPIdentity;


			return company;
		}

		public void UpdateCompanyReference(string ilanglCompanyId, int LWcompanyId, IDbTransaction transaction)
		{
			dtoXMLStorage.CompanyRow cr = Storage.Company.FindById(ilanglCompanyId);

			if (cr == null)
				return;

			cr.TPIdentity = LWcompanyId.ToString();
			cr.TimeSpan = CurrentTimeSpan;


			if (transaction == null)
			{
				AcceptChanges();
			}

		}


		public void UpdateBillingInfoReference(string billingInfoId, int invoiceAccountId, IDbTransaction transaction)
		{
			dtoXMLStorage.BillingInfoRow bir = Storage.BillingInfo.FindById(billingInfoId);

			if (bir == null)
				return;

			bir.TPIdentity = invoiceAccountId.ToString();
			bir.TimeSpan = CurrentTimeSpan;

			if (transaction == null)
			{
				AcceptChanges();
			}
		}


		public BillingInfo ReadBillingInfo(string billingId)
		{
			dtoXMLStorage.BillingInfoRow bir = Storage.BillingInfo.FindById(billingId);
			BillingInfo billinginfo = new BillingInfo();

			billinginfo.Company = new Company();
			billinginfo.Company.Id = bir.CompanyId;

			billinginfo.Id = bir.Id;
			if (!bir.IsNameNull())
				billinginfo.Name = bir.Name;
			if (!bir.IsBillingFirstNameNull())
				billinginfo.BillingFirstName = bir.BillingFirstName;
			if (!bir.IsBillingLastNameNull())
				billinginfo.BillingLastName = bir.BillingLastName;
			if (!bir.IsBillingEmailNull())
				billinginfo.BillingEmail = bir.BillingEmail;
			if (!bir.IsBillingCountryNull())
				billinginfo.BillingCountry = bir.BillingCountry;
			if (!bir.IsBillingStreetAddressNull())
				billinginfo.BillingStreetAddress = bir.BillingStreetAddress;
			if (!bir.IsBillingStreetAddress2Null())
				billinginfo.BillingStreetAddress2 = bir.BillingStreetAddress2;
			if (!bir.IsBillingPostalCodeNull())
				billinginfo.BillingPostalCode = bir.BillingPostalCode;
			if (!bir.IsBillingCityNull())
				billinginfo.BillingCity = bir.BillingCity;
			if (!bir.IsBillingCityNull())
				billinginfo.BillingCurrency = bir.BillingCurrency;
			if (!bir.IsTPIdentityNull())
				billinginfo.TPIdentity = bir.TPIdentity;

		return billinginfo;
		}

		#endregion

		#region ICompanyDAL Members


		public void UpdateCompany(Company company, IDbTransaction transaction)
		{
			CreateCompany(company, transaction);
		}

		#endregion
	}
}
